package com.yanqu.road.server.gameplayer.module.activity.seacraft;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.pb.activity.SeacraftSouthSeaProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Description  海上争霸 之 南海丝路
 * @Author cwq
 * @Data 2022/1/19 14:50
 */
public class SeacraftSouthSeaModule extends GeneralModule {

    public boolean inActivity;

    public SeacraftSouthSeaModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public void afterLogin() {
        sendConfigToClient();
        synUserData();
    }

    @Override
    public boolean saveData() {
        return true;
    }

    public void initSystem() {
        sendConfigToClient();
        synUserData();
    }

    public void synUserData() {
        // 向跨服 获取玩家数据
        if (SeacraftSouthSeaMgr.getActivityInfo() == null || !SystemOpenMgr.systemOpen(player, eSystemId.Seacraft.getValue()) || SeacraftSouthSeaMgr.getConfig() == null) {
            return;
        }
        if (!ActivityMgr.activityInShowTime(SeacraftSouthSeaMgr.getActivityInfo())) {
            return;
        }
        SeacraftSouthSeaProto.SeacraftSouthSeaGetUserDataGameToCrossMsg.Builder gameToCrossMsg = SeacraftSouthSeaProto.SeacraftSouthSeaGetUserDataGameToCrossMsg.newBuilder();
        gameToCrossMsg.setUnionId(player.getUserInfo().getUnionUid());
        gameToCrossMsg.setActivityId(SeacraftSouthSeaMgr.getActivityInfo().getActivityId());
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_SEACRAFT_GET_USER_DATA, gameToCrossMsg, gameToCrossMsg.getActivityId());
    }

    /**
     * 同步配置
     */
    public void sendConfigToClient() {
        if (SeacraftSouthSeaMgr.getActivityInfo() == null || !SystemOpenMgr.systemOpen(player, eSystemId.Seacraft.getValue()) || SeacraftSouthSeaMgr.getConfig() == null) {
            return;
        }
        player.sendPacket(ClientProtocol.U_SEACRAFT_SOUTH_SEA_CONFIG, SeacraftSouthSeaMgr.builderSeacraftSouthSeaConfig(player.getLanguage()));
    }

    /**
     * 同步玩家积分
     * @param allScore
     */
    public void synUserScore(int allScore){
        if(allScore > 0){
            player.notifyListener(eGamePlayerEventType.SeacraftSouthSeaUserScoreRank.getValue(), allScore);
        }
    }

    /**
     * 战斗结束
     */
    public void battleEnd(SeacraftSouthSeaProto.SeacraftSouthSeaChallengeResult result){
        if(result != null){
            // 加勋章道具
            Property reward = new Property(SeacraftSouthSeaMgr.getConfig().getSEACRAFT_CRAFT_SCORE_ITEM_ID(), result.getGetMedalNum());
            reward.addProperty(PropertyHelper.parseStringToProperty(result.getBattleReward()));
            // 加战斗奖励
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.SeacraftActivity,eLogMoneyType.SeacraftActivityChallengeReward);
            // 加勋章成就
            player.notifyListener(eGamePlayerEventType.SeacraftSouthSeaGetMedalNum.getValue(), result.getGetMedalNum());

        }
    }


}
